package cn.yuemouren.security.core.social;

import cn.yuemouren.security.core.properties.SecurityProperties;
import cn.yuemouren.security.core.social.support.SocialAuthenticationFilterPostProcessor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.encrypt.Encryptors;
import org.springframework.social.config.annotation.EnableSocial;
import org.springframework.social.config.annotation.SocialConfigurerAdapter;
import org.springframework.social.connect.ConnectionFactoryLocator;
import org.springframework.social.connect.ConnectionSignUp;
import org.springframework.social.connect.UsersConnectionRepository;
import org.springframework.social.connect.jdbc.JdbcUsersConnectionRepository;
import org.springframework.social.connect.web.ProviderSignInUtils;
import org.springframework.social.security.SpringSocialConfigurer;

import javax.sql.DataSource;

/**
 * @Author: Timi
 * @Description: TODO
 * @Date: 2020/6/26 15:26
 * @Version: v1.0
 */
@Configuration
@EnableSocial
public class SocialConfig extends SocialConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private SecurityProperties securityProperties;
    @Autowired(required = false)
    private ConnectionSignUp connectionSignUp;
    @Autowired(required = false)
    private SocialAuthenticationFilterPostProcessor socialAuthenticationFilterPostProcessor;

    @Override
    public UsersConnectionRepository getUsersConnectionRepository(ConnectionFactoryLocator connectionFactoryLocator) {
        //数据连接，连接池，加密类型
        JdbcUsersConnectionRepository repository = new JdbcUsersConnectionRepository(dataSource, connectionFactoryLocator, Encryptors.noOpText());
        // 数据库表前缀
        // repository.setTablePrefix("");
        if(null != connectionSignUp){
            //实现三方登录后直接绑定系统用户逻辑
            repository.setConnectionSignUp(connectionSignUp);
        }
        return repository;
    }

    @Bean
    public SpringSocialConfigurer timiSocialConfigurer(){
        TimiSpringSocialConfigurer configurer = new TimiSpringSocialConfigurer(securityProperties.getSocial().getFilterProcessUrl());
        //设置默认注册页面地址
        configurer.signupUrl(securityProperties.getBrowser().getSignUpUrl());
        configurer.setSocialAuthenticationFilterPostProcessor(socialAuthenticationFilterPostProcessor);
        return configurer;
    }

    /**
     * 用户获取spring social信息，也是将userId传送给social的
     * @param connectionFactoryLocator 自动注入
     * @return
     */
    @Bean
    public ProviderSignInUtils timiProviderSignInUtils(ConnectionFactoryLocator connectionFactoryLocator){
        return new ProviderSignInUtils(connectionFactoryLocator,getUsersConnectionRepository(connectionFactoryLocator));
    }
}

/**
 * 建表语句
 * create table UserConnection (userId varchar(255) not null,
 * 	providerId varchar(255) not null,
 * 	providerUserId varchar(255),
 * 	rank int not null,
 * 	displayName varchar(255),
 * 	profileUrl varchar(512),
 * 	imageUrl varchar(512),
 * 	accessToken varchar(512) not null,
 * 	secret varchar(512),
 * 	refreshToken varchar(512),
 * 	expireTime bigint,
 * 	primary key (userId, providerId, providerUserId));
 * create unique index UserConnectionRank on UserConnection(userId, providerId, rank);
 */